const mongoose = require("mongoose");

module.exports.deleteAddressesById = async ({_id}) => {
    return await mongoose.model("addressesModel").deleteOne(_id);
}
module.exports.getDresses = async (msg) => {
    const {userId}=msg;
    if(msg.isuse){
       const {isuse}=msg;
       return await mongoose.model("addressesModel").find({userId:userId,isuse:isuse});
    }
    return await mongoose.model("addressesModel").find({userId:userId}).sort({isuse:-1});
}
module.exports.updateDresses = async ({_id}) => {
     await mongoose.model("addressesModel").updateMany({isuse:false});
    return await mongoose.model("addressesModel").updateOne({_id:_id},{isuse:true});
}
module.exports.addAddresses = async newAddresses => {
    let aa= await mongoose.model("addressesModel").find();
    if(aa.length>0){
        for(let i=0;i<aa.length;i++){
        await mongoose.model("addressesModel").updateOne({_id:aa[i]._id},{isuse:false});
        }
    }
    return await mongoose.model("addressesModel").create(newAddresses);
}